package com.gaoyi.crm.db.dao;

import androidx.room.Dao;
import androidx.room.MapInfo;
import androidx.room.Query;
import androidx.room.RawQuery;
import androidx.sqlite.db.SimpleSQLiteQuery;

import com.gaoyi.crm.db.CrmDatabase;
import com.gaoyi.crm.db.entity.ItemNoStore;
import com.gaoyi.crm.db.entity.PmnItemNo;

import java.util.List;
import java.util.Map;

@Dao
public abstract class PmnItemNoDao extends BaseDao<PmnItemNo> {

    public SimpleSQLiteQuery getItemNoByPmnIdSql(String name, String pmnId) {
        String sql = "SELECT itemNoStore.itemNo,itemNoStore.id" +
                ",category." + name + " AS categoryName" +
                " FROM " + CrmDatabase.TABLE_PMN_ITEM_NO + " AS pmnItemNo " +
                " LEFT JOIN " + CrmDatabase.TABLE_ITEM_NO_STORE + " AS itemNoStore ON pmnItemNo.itemNoId = itemNoStore.id " +
                " LEFT JOIN " + CrmDatabase.TABLE_CATEGORY + " AS category ON category.id = itemNoStore.categoryId " +
                " WHERE pmnItemNo.pmnId = '" + pmnId + "'";
        return new SimpleSQLiteQuery(sql);
    }

    @MapInfo(valueColumn = "categoryName")
    @RawQuery
    public abstract Map<ItemNoStore, String> getItemNoByPmnId(SimpleSQLiteQuery query);

    @Query("DELETE FROM " + CrmDatabase.TABLE_PMN_ITEM_NO + " WHERE pmnId = :pmnId")
    public abstract void deleteByPmnId(String pmnId);
}
